global class CreateDashboardScheduler implements schedulable{    
    global void execute(schedulablecontext sc){
    List<Achieve__c> AllAchList = new List<Achieve__c>();
    List<MedicalDashboard__c> MedList = new List<MedicalDashboard__c>();
    
    
    Integer YearI = Date.today().year();
    String Year;
    Integer MonthI = Date.today().month();
    String Month; 
    
    if(MonthI == 1){
        Year = String.valueof(YearI - 1);
        Month = String.valueof(12); 
    }
    else{
        Year = String.valueof(YearI);
        Month = String.valueof(MonthI - 1);
    }
     
    List<AggregateResult> AllAchResult = [Select sum(Total_conversion_achieve__c)TCA,sum(total_conversion_forecast__c)TCF,
        sum(brand_use_in_hospital_cover_forecast__c)BUIHCF,sum(Brand_use_in_hospital_cover_achieve__c)BUIHCA,
        sum(P_class_cover_achieve__c)PCCA,sum(P_class_cover_forecast__c)PCCF,sum(HCP_evaluation_cover_achieve__c)HECA,
        sum(HCP_evaluation_cover_forecast__c)HECF,ownerid own From Achieve__c Where
        Month__c =: Month and Year__c =: Year group by ownerid ];
   
    
    AllAchList = [Select Total_conversion_achieve__c,total_conversion_forecast__c,brand_use_in_hospital_cover_forecast__c,
        Brand_use_in_hospital_cover_achieve__c,P_class_cover_achieve__c,P_class_cover_forecast__c,
        HCP_evaluation_cover_achieve__c,HCP_evaluation_cover_forecast__c,ownerid From Achieve__c Where
        Month__c =: Month and Year__c =: Year];
        
    List<User> MSRList = [Select id,Alias  From User Where FunctionDepartment__c =: 'Medical Sales'
        and Title__c =: 'Sales Rep'];
    for(user msr : MSRList)
    {
        MedicalDashboard__c  inRecord = new MedicalDashboard__c();
        for(AggregateResult agg : AllAchResult)
        {
            if(String.valueof(agg.get('own')) == msr.id)
            {
                inRecord.OwnerId = msr.id;

                if((double)agg.get('BUIHCA') > 0)
                {
                    inRecord.Brand_use_in_hospital_cover_actual__c = (double)agg.get('BUIHCA');                     
                }
                else
                {
                	inRecord.Brand_use_in_hospital_cover_actual__c = 0;
                }

                if((double)agg.get('BUIHCF') > 0)
                {
                    inRecord.Brand_use_in_hospital_cover_forecast__c = (double)agg.get('BUIHCF');                     
                }
                else
                {
                	inRecord.Brand_use_in_hospital_cover_forecast__c = 0 ;
                }

               

                if((double)agg.get('HECA') > 0)
                {
                    inRecord.HCP_evaluation_cover_actual__c = (double)agg.get('HECA');                     
                }
                else
                {
                	inRecord.HCP_evaluation_cover_actual__c = 0;
                }
                

                if((double)agg.get('HECF') > 0)
                {
                    inRecord.HCP_evaluation_cover_forecast__c = (double)agg.get('HECF');                     
                }
                else
                {
                	inRecord.HCP_evaluation_cover_forecast__c = 0;
                }
               
                    if((double)agg.get('PCCA') > 0)
                    {
                        inRecord.P_class_cover_actual__c = (double)agg.get('PCCA');                     
                    }
                    else
                    {
                    	inRecord.P_class_cover_actual__c = 0;
                    }
                 

                    if((double)agg.get('PCCF') > 0)
                    {
                        inRecord.P_class_cover_forecast__c = (double)agg.get('PCCF');                     
                    }
                    else
                    {
                    	inRecord.P_class_cover_forecast__c = 0;
                    }
               

                    if((double)agg.get('TCA') > 0)
                    {
                        inRecord.total_conversion_actual__c = (double)agg.get('TCA');                     
                    }
                    else
                    {
                    	inRecord.total_conversion_actual__c = 0;
                    }
               

                    if((double)agg.get('TCF') > 0)
                    {
                        inRecord.total_conversion_forecast__c = (double)agg.get('TCF');                     
                    }
                    else
                    {
                    	inRecord.total_conversion_forecast__c = 0;
                    }

                inRecord.year__c = Year;
                inRecord.month__c = Month;
                MedList.add(inRecord);                 
                
            }
        }
        
        
/*        
        for(Achieve__c record : AllAchList)
        {
            if(record.ownerid == msr.id)
            {
                inRecord.OwnerId = msr.id;
                if(inRecord.Brand_use_in_hospital_cover_actual__c > 0)
                    inRecord.Brand_use_in_hospital_cover_actual__c = inRecord.Brand_use_in_hospital_cover_actual__c + record.Brand_use_in_hospital_cover_achieve__c;
                else{
                    inRecord.Brand_use_in_hospital_cover_actual__c = record.Brand_use_in_hospital_cover_achieve__c;
                }
                if(inRecord.Brand_use_in_hospital_cover_forecast__c > 0){
                    inRecord.Brand_use_in_hospital_cover_forecast__c = inRecord.Brand_use_in_hospital_cover_forecast__c + record.brand_use_in_hospital_cover_forecast__c;
                }
                else{
                    inRecord.Brand_use_in_hospital_cover_forecast__c = record.brand_use_in_hospital_cover_forecast__c;
                }
                if(inRecord.HCP_evaluation_cover_actual__c > 0){
                    inRecord.HCP_evaluation_cover_actual__c = inRecord.HCP_evaluation_cover_actual__c + record.HCP_evaluation_cover_achieve__c;
                }
                else{
                    inRecord.HCP_evaluation_cover_actual__c = record.HCP_evaluation_cover_achieve__c;
                }
                if(inRecord.HCP_evaluation_cover_forecast__c > 0){
                    inRecord.HCP_evaluation_cover_forecast__c = inRecord.HCP_evaluation_cover_forecast__c + record.HCP_evaluation_cover_forecast__c;
                }
                else{
                    inRecord.HCP_evaluation_cover_forecast__c = record.HCP_evaluation_cover_forecast__c;
                }
                if(inRecord.P_class_cover_actual__c > 0){
                    inRecord.P_class_cover_actual__c = inRecord.P_class_cover_actual__c + record.P_class_cover_achieve__c;
                }
                else{
                    inRecord.P_class_cover_actual__c = record.P_class_cover_achieve__c;
                }
                if(inRecord.P_class_cover_forecast__c > 0){
                    inRecord.P_class_cover_forecast__c = inRecord.P_class_cover_forecast__c + record.P_class_cover_forecast__c;
                }
                else{
                    inRecord.P_class_cover_forecast__c = record.P_class_cover_forecast__c;
                }                               
                if(inRecord.total_conversion_actual__c > 0){
                    inRecord.total_conversion_actual__c = inRecord.total_conversion_actual__c + record.Total_conversion_achieve__c;
                }
                else{
                    inRecord.total_conversion_actual__c = record.Total_conversion_achieve__c;
                }                 
                if(inRecord.total_conversion_forecast__c > 0){
                    inRecord.total_conversion_forecast__c = inRecord.total_conversion_forecast__c + record.total_conversion_forecast__c;
                }
                else{
                    inRecord.total_conversion_forecast__c = record.total_conversion_forecast__c;
                }                               
                inRecord.year__c = Year;
                inRecord.month__c = Month;
                MedList.add(inRecord);          
            }
        }
        */
         
    }
    insert MedList;
    
    }
}